package com.storm.lib.task;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import com.storm.lib.base.BaseTask;
import com.storm.lib.template.MapAvatarTemplate;
import com.storm.lib.util.BeanUtil;

/**
 * 
 * 类功能:同步玩家的地图信息
 * @author johnny
 * @version 2011-8-23
 */
public class SyncMapAvatarTask extends BaseTask{

	@Override
	public void run() {
		MapAvatarTemplate mapAvatarTemplate = (MapAvatarTemplate) BeanUtil.getBean("mapAvatarTemplate");
		List<Object[]> toUpdate = null;
		//遍历每个玩家的列表
		int total = 0;
		for (Integer playerToUpdateListId : mapAvatarTemplate.getAroundAvatarCache().keySet()) {
			ConcurrentHashMap<Integer,ConcurrentHashMap<Integer,Object[]>> playerToUpdateList = mapAvatarTemplate.getAroundAvatarCache().get(playerToUpdateListId);
			
			//遍历每个玩家的每一个目标项
			toUpdate =null;
			for (ConcurrentHashMap<Integer,Object[]> targetPlayer : playerToUpdateList.values()) {
				for (Object[] singleObjects : targetPlayer.values()) {
					if(toUpdate==null){
						toUpdate = new ArrayList<Object[]>();
					}
					toUpdate.add(singleObjects);
				}
			}
			total++;
			mapAvatarTemplate.flushOutChanges(playerToUpdateListId,toUpdate);
		}
//		System.out.println("广播通知个玩家"+total);
	}

	public SyncMapAvatarTask() {
		this.setRepeatDelayMS(5111);
		this.setFirstDelayMS(1000);
	}

}
